﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Microsoft.Xna.Framework;
using System.IO;
using System.Threading;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace ParticleEditor
{
    public partial class particleEditor : Form
    {

        public Game1PE game;
        private int maxEmitterAmount = 0;
        delegate void setControlsInfo();//used to thread-cross

        public particleEditor()
        {
            InitializeComponent();

            timer1.Enabled = true;
            timer1.Interval = 100;
        }

        public void initImages()
        {
            labelINFO.Text = "";
            buttonStop.Image = Texture2Image(game.stop);
            buttonRefresh.Image = Texture2Image(game.refresh1x);
            buttonBG.Image = Texture2Image(game.BG);
            buttonRespawn.Image = Texture2Image(game.refresh);
            timer1.Enabled = false;
            
        }

        private void particleEditor_FormClosed(object sender, FormClosedEventArgs e)
        {
            game.Exit();
            Application.Exit();
        }

        #region tabs info initialization
        public void setParticleInfo()
        {
            textBoxPLifetimeRFrom.Text = game.helper.emitter.LifeTime_Random_Start.ToString();
            textBoxPLifetimeRTo.Text = game.helper.emitter.LifeTime_Random_End.ToString();

            textBoxPVelocityRFrom.Text = game.helper.emitter.Velocity_Random_Start.ToString();
            textBoxPVelocityRTo.Text = game.helper.emitter.Velocity_Random_end.ToString();

            textBoxPAlphaStartRFrom.Text = game.helper.emitter.Alpha_Random_Start_Start.ToString();
            textBoxPAlphaStartRTo.Text = game.helper.emitter.Alpha_Random_Start_End.ToString();
            textBoxPAlphaREndFrom.Text = game.helper.emitter.Alpha_Random_End_Start.ToString();
            textBoxPAlphaREndTo.Text = game.helper.emitter.Alpha_Random_End_End.ToString();

            textBoxPScaleStartRFrom.Text = game.helper.emitter.Scale_Random_Start_Start.ToString();
            textBoxPScaleStartRTo.Text = game.helper.emitter.Scale_Random_Start_End.ToString();
            textBoxPScaleREndFrom.Text = game.helper.emitter.Scale_Random_End_Start.ToString();
            textBoxPScaleREndTo.Text = game.helper.emitter.Scale_Random_End_End.ToString();

            textBoxPRotationRFrom.Text = game.helper.emitter.Rotation_Random_Start.ToString();
            textBoxPRotationRTo.Text = game.helper.emitter.Rotation_Random_End.ToString();
        }

        private void setEmitterInfo()
        {
            //EMITTER PROPERTIES
            textBoxEName.Text = game.helper.emitter.Name;
            for (int i = 0; i < game.helper.Images.Count; i++)
                comboBoxEImage.Items.Add(i);
            comboBoxEImage.SelectedItem = comboBoxEImage.Items[game.helper.emitter.Image];

            textBoxEGravity.Text = game.helper.emitter.Gravity.ToString();
            textBoxEWind.Text = game.helper.emitter.Wind.ToString();
            textBoxEAngleRFrom.Text = game.helper.emitter.Angle_Random_Start.ToString();
            textBoxEAngleRTo.Text = game.helper.emitter.Angle_Random_End.ToString();
            textBoxERate.Text = game.helper.emitter.Rate.ToString();

            textBoxEAmount.Text = game.helper.emitter.Amount_Max.ToString();
            maxEmitterAmount = game.helper.emitter.Amount_Max;  //to keep the real max value in case
            label38.Text = "/" + maxEmitterAmount;

            textBoxEWidth.Text = game.helper.emitter.Width.ToString();
            textBoxEHeight.Text = game.helper.emitter.Height.ToString();

            if (game.helper.emitter.useBoundingBox == true)
                useBoundingBox.CheckState = CheckState.Checked;
            if (game.helper.emitter.useBoundingBox == false)
                useBoundingBox.CheckState = CheckState.Unchecked;
        }
        #endregion

        #region Emitter tab lostFocus events
        private void textBoxEName_LostFocus(object sender, EventArgs e)
        {
            try
            {
                if(game.helper.emitter.Name != textBoxEName.Text)
                    game.helper.emitter.Name = textBoxEName.Text;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        //unused
        private void textBoxEGravity_LostFocus(object sender, EventArgs e)
        {
            try
            {
                if (game.helper.emitter.Gravity != float.Parse(textBoxEGravity.Text))
                    game.helper.emitter.Gravity = float.Parse(textBoxEGravity.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        //unused
        private void textBoxEWind_LostFocus(object sender, EventArgs e)
        {
            try
            {
                if (game.helper.emitter.Wind != float.Parse(textBoxEWind.Text))
                    game.helper.emitter.Wind = float.Parse(textBoxEWind.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void textBoxEAngle_LostFocus(object sender, EventArgs e)
        {
            try
            {
                if (game.helper.emitter.Angle_Random_Start != int.Parse(textBoxEAngleRFrom.Text) || game.helper.emitter.Angle_Random_End != int.Parse(textBoxEAngleRTo.Text))
                {
                    game.helper.emitter.updateParticlesAngle(int.Parse(textBoxEAngleRFrom.Text), int.Parse(textBoxEAngleRTo.Text));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void textBoxERate_LostFocus(object sender, EventArgs e)
        {
            try
            {
                if(game.helper.emitter.Rate != float.Parse(textBoxERate.Text))
                    game.helper.emitter.Rate = float.Parse(textBoxERate.Text);
                Console.WriteLine(game.helper.emitter.Rate);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void textBoxEAmount_LostFocus(object sender, EventArgs e)
        {
            try
            {
                if (int.Parse(textBoxEAmount.Text) > maxEmitterAmount)  //maxEmitterAmount was assign on the creation of the effect
                {
                    //MessageBox.Show("Max amount can be changed only on creation");
                    textBoxEAmount.Text = maxEmitterAmount.ToString();
                }
                else
                {
                    if (game.helper.emitter.Amount_Max != int.Parse(textBoxEAmount.Text))
                    {
                        game.helper.emitter.Amount_Max = int.Parse(textBoxEAmount.Text);
                        game.helper.emitter.updateParticlesAngle(int.Parse(textBoxEAngleRFrom.Text), int.Parse(textBoxEAngleRTo.Text));
                        game.helper.emitter.Respawn();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void textBoxEWidth_LostFocus(object sender, EventArgs e)
        {
            try
            {
                if (game.helper.emitter.Width != int.Parse(textBoxEWidth.Text))
                {
                    game.helper.emitter.Width = int.Parse(textBoxEWidth.Text);
                    game.helper.updateSizeRect();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void textBoxEHeight_LostFocus(object sender, EventArgs e)
        {
            try
            {
                if (game.helper.emitter.Height != int.Parse(textBoxEHeight.Text))
                {
                    game.helper.emitter.Height = int.Parse(textBoxEHeight.Text);
                    game.helper.updateSizeRect();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void comboBoxEImage_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                game.helper.emitter.Image = int.Parse(comboBoxEImage.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region Particle tab LostFocus events
        //lifetimeRfrom and lifetimeRTo calls the same method in emitter to update
        private void textBoxPLifetime_Leave(object sender, EventArgs e)
        {
            try
            {
                if (game.helper.emitter.LifeTime_Random_Start != int.Parse(textBoxPLifetimeRFrom.Text) || game.helper.emitter.LifeTime_Random_End != int.Parse(textBoxPLifetimeRTo.Text))
                {
                    if (int.Parse(textBoxPLifetimeRFrom.Text) > int.Parse(textBoxPLifetimeRTo.Text))
                    {
                        textBoxPLifetimeRFrom.Text = textBoxPLifetimeRTo.Text;  //cheking and if from is higher that too from becomes too
                    }
                        game.helper.emitter.updateParticlesLifetime(int.Parse(textBoxPLifetimeRFrom.Text), int.Parse(textBoxPLifetimeRTo.Text));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        //
        private void textBoxPVelocity_Leave(object sender, EventArgs e)
        {
            try
            {
                if (game.helper.emitter.Velocity_Random_Start != float.Parse(textBoxPVelocityRFrom.Text) || game.helper.emitter.Velocity_Random_end != float.Parse(textBoxPVelocityRTo.Text))
                {
                    if (float.Parse(textBoxPVelocityRFrom.Text) > float.Parse(textBoxPVelocityRTo.Text))
                    {
                        textBoxPVelocityRFrom.Text = textBoxPVelocityRTo.Text;
                    }
                        game.helper.emitter.updateParticlesVelocity(float.Parse(textBoxPVelocityRFrom.Text), float.Parse(textBoxPVelocityRTo.Text));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        //is shooted when we update start and end
        private void textBoxPAlphaStart_Leave(object sender, EventArgs e)
        {
            try
            {
                if (game.helper.emitter.Alpha_Random_Start_Start != float.Parse(textBoxPAlphaStartRFrom.Text) || game.helper.emitter.Alpha_Random_Start_End != float.Parse(textBoxPAlphaStartRTo.Text))
                {
                    if (float.Parse(textBoxPAlphaStartRFrom.Text) > float.Parse(textBoxPAlphaStartRTo.Text))
                    {
                        textBoxPAlphaStartRFrom.Text = textBoxPAlphaStartRTo.Text;
                    }
                        game.helper.emitter.updateParticlesAlphaBegin(float.Parse(textBoxPAlphaStartRFrom.Text), float.Parse(textBoxPAlphaStartRTo.Text));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        //
        private void textBoxPAlphaEnd_Leave(object sender, EventArgs e)
        {
            try
            {
                if (game.helper.emitter.Alpha_Random_End_Start != float.Parse(textBoxPAlphaREndFrom.Text) || game.helper.emitter.Alpha_Random_End_End != float.Parse(textBoxPAlphaREndTo.Text))
                {
                    if (float.Parse(textBoxPAlphaREndFrom.Text) > float.Parse(textBoxPAlphaREndTo.Text))
                    {
                        textBoxPAlphaREndFrom.Text = textBoxPAlphaREndTo.Text;
                    }
                    game.helper.emitter.updateParticlesAlphaEnd(float.Parse(textBoxPAlphaREndFrom.Text), float.Parse(textBoxPAlphaREndTo.Text));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        //
        private void textBoxPScaleBegin_Leave(object sender, EventArgs e)
        {
            try
            {
                if (game.helper.emitter.Scale_Random_Start_Start != float.Parse(textBoxPScaleStartRFrom.Text) || game.helper.emitter.Scale_Random_Start_End != float.Parse(textBoxPScaleStartRTo.Text))
                {
                    if(float.Parse(textBoxPScaleStartRFrom.Text) > float.Parse(textBoxPScaleStartRTo.Text))
                    {
                        textBoxPScaleStartRFrom.Text = textBoxPScaleStartRTo.Text;
                    }
                    game.helper.emitter.updateParticlesScaleBegin(float.Parse(textBoxPScaleStartRFrom.Text), float.Parse(textBoxPScaleStartRTo.Text));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        //
        private void textBoxPScaleEnd_Leave(object sender, EventArgs e)
        {
            try
            {
                if (game.helper.emitter.Scale_Random_End_Start != float.Parse(textBoxPScaleREndFrom.Text) || game.helper.emitter.Scale_Random_End_End != float.Parse(textBoxPScaleREndTo.Text))
                {
                    if (float.Parse(textBoxPScaleREndFrom.Text) > float.Parse(textBoxPScaleREndTo.Text))
                    {
                        textBoxPScaleREndFrom.Text = textBoxPScaleREndTo.Text;
                    }
                    game.helper.emitter.updateParticlesScaleEnd(float.Parse(textBoxPScaleREndFrom.Text), float.Parse(textBoxPScaleREndTo.Text));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        //
        private void textBoxPRotation_Leave(object sender, EventArgs e)
        {
            try
            {
                if (game.helper.emitter.Rotation_Random_Start != float.Parse(textBoxPRotationRFrom.Text) || game.helper.emitter.Rotation_Random_End != float.Parse(textBoxPRotationRTo.Text))
                {
                    if (float.Parse(textBoxPRotationRFrom.Text) > float.Parse(textBoxPRotationRTo.Text))
                    {
                        textBoxPRotationRFrom.Text = textBoxPRotationRTo.Text;
                    }
                    game.helper.emitter.updateParticlesRotationIncr(float.Parse(textBoxPRotationRFrom.Text), float.Parse(textBoxPRotationRTo.Text));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region Loading Image
        private void button4_Click(object sender, EventArgs e)
        {
            Thread newThread = new Thread(new ThreadStart(openDialog));
            newThread.SetApartmentState(ApartmentState.STA);
            newThread.Start();
        }

        private void openDialog()
        {
            OpenFileDialog fileDialog = new OpenFileDialog();

            fileDialog.InitialDirectory = Environment.CurrentDirectory;
            fileDialog.Title = "Load Image";

            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                loadContentImage(fileDialog.FileName);
            }
        }

        private void loadContentImage(string stream)
        {
            FileStream fs = new FileStream(stream, FileMode.Open);
            Texture2D t2d = Texture2D.FromStream(game.GraphicsDevice, fs);
            fs.Close();

            game.helper.Images.Add(t2d);
            game.helper.emitter.Image = game.helper.Images.IndexOf(t2d);

        }
        #endregion

        #region load background
        private void buttonBG_Click(object sender, EventArgs e)
        {
            Thread newThread = new Thread(new ThreadStart(openDialogBG));
            newThread.SetApartmentState(ApartmentState.STA);
            newThread.Start();
        }

        private void openDialogBG()
        {
            OpenFileDialog fileDialog = new OpenFileDialog();

            fileDialog.InitialDirectory = Environment.CurrentDirectory;
            fileDialog.Title = "Load BG";

            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                loadBG(fileDialog.FileName);
            }
        }

        private void loadBG(string stream)
        {
            FileStream fs = new FileStream(stream, FileMode.Open);
            Texture2D t2d = Texture2D.FromStream(game.GraphicsDevice, fs);
            fs.Close();

            game.helper.Background = t2d;
            game.helper.DrawBackground = true;
        }
        #endregion

        #region Buttons click events
        //http://communistgames.blogspot.com/2010/10/converting-between-texture2d-and-image.html
        public static System.Drawing.Image Texture2Image(Texture2D texture)
        {
            //Memory stream to store the bitmap data.
            MemoryStream ms = new MemoryStream();

            //Save the texture to the stream.
            texture.SaveAsPng(ms, texture.Width, texture.Height);

            //Seek the beginning of the stream.
            ms.Seek(0, SeekOrigin.Begin);

            //Create an image from a stream.
            System.Drawing.Image bmp2 = System.Drawing.Bitmap.FromStream(ms);

            //Close the stream, we nolonger need it.
            ms.Close();
            ms = null;
            return bmp2;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                initImages();
                setParticleInfo();
                setEmitterInfo();
                game.helper.emitter.Start();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private void buttonStop_Click(object sender, EventArgs e)
        {
            if (game.helper.emitter.started == true)
            {
                buttonStop.Image = Texture2Image(game.start);
                game.helper.emitter.Pause();
            }
            else
            {
                buttonStop.Image = Texture2Image(game.stop);
                game.helper.emitter.Start();
            }
        }

        //respawned 1x
        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            game.helper.emitter.Respawn();
        }

        //keep on respawnding
        private void buttonRespawn_Click(object sender, EventArgs e)
        {
            if (game.helper.emitter.AutoRespawn != true)
            {
                game.helper.emitter.AutoRespawn = true;
                buttonRespawn.Image = Texture2Image(game.refreshChecked);
            }
            else
            {
                game.helper.emitter.AutoRespawn = false;
                buttonRespawn.Image = Texture2Image(game.refresh);
            }
        }

        //events to handle info showing when over the controls
        private void LabelInfoClear(object sender, EventArgs e)
        {
            labelINFO.Text = "";
        }
        #endregion

        #region buttons mouseHover and mouseLeave
        private void buttonStop_MouseHover(object sender, EventArgs e)
        {
            labelINFO.Text = "By pressing it you pause/resume effect";
        }

        private void buttonRefresh_MouseHover(object sender, EventArgs e)
        {
            labelINFO.Text = "Restarts the effect";
        }

        private void buttonRespawn_MouseHover(object sender, EventArgs e)
        {
            labelINFO.Text = "Keeps the effect repeating after its finishes";
        }

        private void buttonBG_MouseHover(object sender, EventArgs e)
        {
            labelINFO.Text = "Dialog for select background; Location can by changed on blue screen";
        }
        #endregion

        #region Particle mouseHover and mouseLeave
        private void textBoxPLifetimeRFrom_MouseHover(object sender, EventArgs e)
        {
            labelINFO.Text = "0 - can't be higher than 'To' value; In Miliseconds";
        }

        private void textBoxPLifetimeRTo_MouseHover(object sender, EventArgs e)
        {
            labelINFO.Text = "higher than 'From', if not the same as 'From' will be randomize; In Miliseconds";
        }

        private void textBoxPVelocityRFrom_MouseHover(object sender, EventArgs e)
        {
            labelINFO.Text = "from minus infinity...can't be higher than 'To' value";
        }

        private void textBoxPVelocityRTo_MouseHover(object sender, EventArgs e)
        {
            labelINFO.Text = "higher than 'From', if not the same as 'From' will be randomize";
        }

        private void textBoxPAlphaStartRFrom_MouseHover(object sender, EventArgs e)
        {
            labelINFO.Text = "values from 0-100% can't be higher than 'To' value";
        }

        private void textBoxPAlphaStartRTo_MouseHover(object sender, EventArgs e)
        {
            labelINFO.Text = "higher than 'From', if not the same as 'From' will be randomize";
        }

        private void textBoxPAlphaREndFrom_MouseHover(object sender, EventArgs e)
        {
            labelINFO.Text = "values from 0-100% can't be higher than 'To' value";
        }

        private void textBoxPAlphaREndTo_MouseHover(object sender, EventArgs e)
        {
            labelINFO.Text = "higher than 'From', if not the same as 'From' will be randomize";
        }

        private void textBoxPScaleStartRFrom_MouseHover(object sender, EventArgs e)
        {
            labelINFO.Text = "values from 0-100% can't be higher than 'To' value";
        }

        private void textBoxPScaleStartRTo_MouseHover(object sender, EventArgs e)
        {
            labelINFO.Text = "higher than 'From', if not the same as 'From' will be randomize";
        }

        private void textBoxPScaleREndFrom_MouseHover(object sender, EventArgs e)
        {
            labelINFO.Text = "values from 0-100% can't be higher than 'To' value";
        }

        private void textBoxPScaleREndTo_MouseHover(object sender, EventArgs e)
        {
            labelINFO.Text = "higher than 'From', if not the same as 'From' will be randomize";
        }

        private void textBoxPRotationRFrom_MouseHover(object sender, EventArgs e)
        {
            labelINFO.Text = "no limits, if will be '-' will go anti clockwise";
        }

        private void textBoxPRotationRTo_MouseHover(object sender, EventArgs e)
        {
            labelINFO.Text = "higher than 'From', if not the same as 'From' will be randomize";
        }
        #endregion

        //when mouse on xna window
        //also handles that when you are here you can change bg position
        private void pctSurface_MouseHover(object sender, EventArgs e)
        {
            //labelINFO.Text = "Red lines represent bounding rectangle, inner lines - X,Y axis";
            labelINFO.Text = "By clicking you can change location of background top left corner";
            game.helper.changeBGPosition = true;
        }

        #region emitter tab info
        private void textBoxEName_MouseHover(object sender, EventArgs e)
        {
            labelINFO.Text = "name on which the XML file will be saved";
        }

        private void comboBoxEImage_MouseHover(object sender, EventArgs e)
        {
            labelINFO.Text = "list of basic shapes existing already in the editor";
        }

        private void button4_MouseHover(object sender, EventArgs e)
        {
            labelINFO.Text = "Load any kind of texture you want to use as a particles";
        }

        private void textBoxEGravity_MouseHover(object sender, EventArgs e)
        {
            labelINFO.Text = "values 0 - n; keeps decreasing the speed till particles starts to fall";
        }

        private void textBoxEWind_MouseHover(object sender, EventArgs e)
        {
            labelINFO.Text = "NOT IMPLEMENTED YET...";
        }

        private void textBoxEAngleRFrom_MouseHover(object sender, EventArgs e)
        {
            labelINFO.Text = "from 0 to less that 'To'. Presents spreading angle beginning";
        }

        private void textBoxEAngleRTo_MouseHover(object sender, EventArgs e)
        {
            labelINFO.Text = "from 'From' values till 360, present spreading angle end";
        }

        private void textBoxERate_MouseHover(object sender, EventArgs e)
        {
            labelINFO.Text = "from 0 infinity. Represents delay between particles spawned";
        }

        private void textBoxEAmount_MouseHover(object sender, EventArgs e)
        {
            labelINFO.Text = "from 0 till 1000, present the number of particles used.";
        }

        private void textBoxEWidth_MouseHover(object sender, EventArgs e)
        {
            labelINFO.Text = "The width of particles bouding box; from 0 till infinity";
        }

        private void textBoxEHeight_MouseHover(object sender, EventArgs e)
        {
            labelINFO.Text = "The height of particles bouding box; from 0 till infinity";
        }

        private void trackBar1_ValueChanged(object sender, EventArgs e)
        {
            int trackIndex = trackBar1.Value;
            switch (trackIndex)
            {
                case (0):
                    game.helper.emitter.playingSpeedRate = 0.35f;
                    break;
                case (1):
                    game.helper.emitter.playingSpeedRate = 0.15f;
                    break;
                case (2):
                    game.helper.emitter.playingSpeedRate = 0.10f;
                    break;
                case (3):
                    game.helper.emitter.playingSpeedRate = 0.05f;
                    break;
                case (4):
                    game.helper.emitter.playingSpeedRate = 0.0f;
                    break;
            }
        }

        private void trackBar1_MouseHover(object sender, EventArgs e)
        {
            labelINFO.Text = "PLayback speed, controls pauses between update circles";
        }
        #endregion

        private void pctSurface_MouseLeave(object sender, EventArgs e)
        {
            labelINFO.Text = "";
            game.helper.changeBGPosition = false;
        }

        // handles serialization invoke and saves it to desired location
        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Thread newThread = new Thread(new ThreadStart(openDialogSave));
            newThread.SetApartmentState(ApartmentState.STA);
            newThread.Start();
        }

        private void openDialogSave()
        {
            SaveFileDialog fileDialog = new SaveFileDialog();

            fileDialog.InitialDirectory = Environment.CurrentDirectory;
            fileDialog.Title = "Save Effect";
            fileDialog.Filter = "XML files|*.xml";

            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                //clearing some values
                game.helper.emitter.AutoRespawn = false;
                game.helper.emitter.playingSpeedRate = 0.0f;
                ParticleSystem.RFBase.save(game.helper.emitter, fileDialog.FileName);
            }
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Thread newThread = new Thread(new ThreadStart(openDialogOpen));
            newThread.SetApartmentState(ApartmentState.STA);
            newThread.Start();
        }

        private void openDialogOpen()
        {
            OpenFileDialog fileDialog = new OpenFileDialog();

            fileDialog.InitialDirectory = Environment.CurrentDirectory;
            fileDialog.Title = "Open Effect";
            fileDialog.Tag = "*.xml";

            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                string fullPath = fileDialog.FileName;
                Console.WriteLine(fullPath);

                if (File.Exists(fullPath))
                {
                    game.helper.emitter = (ParticleSystem.Emitter)ParticleSystem.RFBase.load(fullPath, typeof(ParticleSystem.Emitter));
                    game.helper.emitter.AutoRespawn = true;
                    setControlsInfo SCI = new setControlsInfo(setParticleInfo);
                    this.Invoke(SCI);
                    setControlsInfo SCI2 = new setControlsInfo(setEmitterInfo);
                    this.Invoke(SCI2);
                }
            }
        }

        private void useBoundingBox_CheckedChanged(object sender, EventArgs e)
        {
            if (useBoundingBox.CheckState == CheckState.Checked)
                game.helper.emitter.useBoundingBox = true;
            if (useBoundingBox.CheckState == CheckState.Unchecked)
                game.helper.emitter.useBoundingBox = false;
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Particle Editor 1.0 \nHave the most of the functionality in basic form, development still in progress..");
        }

        private void useBoundingBox_MouseHover(object sender, EventArgs e)
        {
            labelINFO.Text = "When checked draws and uses bounding box.";
        }


        //To update XNA window once the size of GUI changes
        private void pctSurface_SizeChanged(object sender, EventArgs e)
        {
            game.helper.updateEmitterPosition(pctSurface.Size.Width, pctSurface.Size.Height);
        }
    }
}
